package com.example.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.example.myapplication.R;

public class ProfileFragment extends Fragment {
    private UserViewModel userViewModel;
    private TextView tvUsernameContent; // 显示用户名

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 加载个人资料布局
        View view = inflater.inflate(R.layout.fragment_profile_simple, container, false);

        // 绑定用户名控件
        tvUsernameContent = view.findViewById(R.id.tv_username_content);

        // 关联ViewModel，观察用户名变化
        userViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);
        userViewModel.getUsername().observe(getViewLifecycleOwner(), username -> {
            tvUsernameContent.setText(username != null && !username.isEmpty() ? username : "未登录");
        });

        return view;
    }
}